﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title =T("admin.productvarinatattribute.create.title");
    Layout = "~/views/shared/_adminLayout.cshtml";
}

<h2>@T("admin.productvarinatattribute.create.title")</h2>

<div>
    <label>@T("admin.productvariantattribute.attribute"):</label>
    <select data-bind="options: AvailableProductAttributes, optionsText: 'Name', value: SelectedProductAttribute"></select>
</div>
<div>
    <label>@T("admin.productvariantattribute.attributecontroltype"):</label>
    <select data-bind="options: AvailableAttributeControlTypes, optionsText: 'ControlTypeName', value: SelectedAttributeType"></select>
    
</div>

<div>
    
    <input type="button"  data-bind="value:'@T("Admin.ProductVariantAttribute.Create.Button.Title")', click:SaveProductVarianAttribute" class="btn btn-primary"/>

<a  data-bind="attr:{href:'/admin/ProductVariant/edit?productVariantId='+PvId()+'&tab=pva'}" class="btn btn-link">@T("Admin.ProductVariantAttribute.Save.Back")</a>
</div>
@{
    
    var model = ViewBag.Model;
    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}

@section scripts
{
    <script>
        var init = @jsonStr;

        
        function AttributeControlType(controlTypeName, controlTypeValue) {
            this.ControlTypeName = ko.observable(controlTypeName);
            this.ControlTypeValue= ko.observable(controlTypeValue);
        }

        function ProductAttribute(id, name) {

            this.Id = ko.observable(id);
            this.Name= ko.observable(name);
        }
        
        function ProductVariantAttributeModel() {

            var self = this;
            self.PvId = ko.observable(init.PvId);

            var cts =  $.map(init.AvailableAttributeControlTypes, function (a) {
                return new AttributeControlType(a.ControlTypeName, a.ControlTypeValue);
            });

            self.AvailableAttributeControlTypes = ko.observableArray(cts);

            self.SelectedAttributeType = ko.observable();


            var apa= $.map(init.AvailableProductAttributes, function (a) {
                return new ProductAttribute(a.Id, a.Name);
            });

            self.AvailableProductAttributes = ko.observableArray(apa);

            self.SelectedProductAttribute= ko.observable();
            
            self.SaveProductVarianAttribute= function() {
                $.ajax({
                    url: "/api/ProductVariantAttributeApi/InsertProductVariantAttribute",
                    type: 'post',
                    data: ko.toJSON(self),
                    contentType: 'application/json',
                    success: function (result) {

                        if (result.Status == "0") {
                            location.href = '/admin/ProductVariant/Edit?productVariantId=' + self.PvId()+"&tab=pva";
                        }
                        else {
                            toastr.error(result.Message);
                        }
                    }
                });
            };
        }

        var vm = new ProductVariantAttributeModel();
        ko.applyBindings(vm);
    </script>


}

